
package com.rx.mgt.controller;  

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.rx.mgt.domain.AdminUser;
import com.rx.mgt.domain.ScheduleTask;
import com.rx.mgt.domain.UserInfoExt;
import com.rx.mgt.po.UsersBo;
import com.rx.mgt.service.DomainService;
import com.rx.mgt.service.ScheduleTaskService;
import com.rx.mgt.service.SysLogService;
import com.rx.mgt.util.IpUtil;

/** 
 * ClassName:ScheduleTaskController 
 * Function :
 * Reason   : 
 * Date     :Feb 6, 2017 9:30:42 AM 
 * @author   luolei 
 * @since    JDK 1.6
 */
@Controller
public class ScheduleTaskController {
    @Autowired
    ScheduleTaskService scheduleTaskService ;
    @Autowired
    DomainService domainService;
    @Autowired
    SysLogService sysLogService;
    
    @RequestMapping(value="/sys-task-list.htmlx")
    public String  sysTaskList(ModelMap model,UsersBo usersBo){
        List list = scheduleTaskService.getAllTask(usersBo);
        model.addAttribute("list",list);
        model.addAttribute("usersBo",usersBo);
        System.out.println("111111111111111");
        return "sys/sys-task-list";
    }
    @SuppressWarnings({ "rawtypes", "unchecked" })
    @RequestMapping(value="/sys-task-add.htmlx")
    public String  sysTaskAdd(ModelMap model,HttpServletRequest request,ScheduleTask scheduleTask,UsersBo usersBo){
        AdminUser user = (AdminUser) request.getSession().getAttribute("user");
        if("2".equals(user.getType())){
            usersBo.setDomain(user.getDomain());
        }
        List list = domainService.getDomainInfoExt(usersBo);
        model.addAttribute("domainList",list);
        if(scheduleTask.getId()==null||scheduleTask.getId()<1){
            model.addAttribute("task",scheduleTask);
            model.addAttribute("method","add");
            return "sys/sys-task-add";
        }
        ScheduleTask info= scheduleTaskService.get(scheduleTask.getId());
        if(info!=null){
            model.addAttribute("task",info);
        }
        System.out.println("111111111111111");
        return "sys/sys-task-add";
    }
    @SuppressWarnings({ "rawtypes", "unchecked" })
    @RequestMapping(value="/taskSave.htmlx")
    @ResponseBody
    public Map  taskSave(HttpServletRequest request ,ModelMap model,ScheduleTask scheduleTask){
        AdminUser user = (AdminUser) request.getSession().getAttribute("user");
        sysLogService.add("更新定时任务", IpUtil.getIpAddr(request), "管理", user.getId()+"", user.getUser_name());
        Map map =new HashMap();
        map.put("result", "success");
        scheduleTaskService.saveOrUpdate(scheduleTask);;
        System.out.println("111111111111111");
        return map;
    }
    
    @SuppressWarnings({ "rawtypes", "unchecked" })
    @RequestMapping(value="/stopTask.htmlx")
    @ResponseBody
    public Map  stopTask(HttpServletRequest request ,ScheduleTask scheduleTask){
        AdminUser user = (AdminUser) request.getSession().getAttribute("user");
        sysLogService.add("停止定时任务", IpUtil.getIpAddr(request), "管理", user.getId()+"", user.getUser_name());
        scheduleTaskService.stopTask(scheduleTask);
        Map map =new HashMap();
        map.put("result", "success");
        return map;
    }
    @SuppressWarnings({ "rawtypes", "unchecked" })
    @RequestMapping(value="/startTask.htmlx")
    @ResponseBody
    public Map  startTask(HttpServletRequest request ,ScheduleTask scheduleTask){
        AdminUser user = (AdminUser) request.getSession().getAttribute("user");
        sysLogService.add("开启定时任务", IpUtil.getIpAddr(request), "管理", user.getId()+"", user.getUser_name());
        scheduleTaskService.startTask(scheduleTask);
        Map map =new HashMap();
        map.put("result", "success");
        return map;
    }
    @SuppressWarnings({ "rawtypes", "unchecked" })
    @RequestMapping(value="/deleteTask.htmlx")
    @ResponseBody
    public Map  deleteTask(HttpServletRequest request ,String ids){
        AdminUser user = (AdminUser) request.getSession().getAttribute("user");
        sysLogService.add("删除定时任务", IpUtil.getIpAddr(request), "管理", user.getId()+"", user.getUser_name());
        Map map =new HashMap();
        if(ids!=null){
            String[] id=ids.split(",");
            for(String s:id){
                scheduleTaskService.delete(Long.valueOf(s));
            }
        }
        map.put("result", "success");
        System.out.println("111111111111111");
        return map;
    }
}
  